﻿/**
* @description  Combines multiple transitions into a single effect.
*/
package com.foed.animation.transitions {

	import com.foed.events.TweenEvent;

	public class CompositeTransition extends Transition {
	
		private var _transitionIndex:uint;
		private var _transitions:Array;
	
		/**
		* @description  Constructor.
		*
		* @param  pTransitions  The array of transitions to run simultaneously.
		*/
		public function CompositeTransition(pTransitions:Array=null) {
			_transitions = pTransitions || [];
		}
		
		/**
		* @description  Adds a transition to the list of transitions to run.
		*
		* @param  pTransition  The transition to add to the list.
		*/
		public function addTransition(pTransition:Transition):void {
			_transitions.push(pTransition);
		}
	
		/**
		* @description  Handler for the event fired at the end of the tween.
		*
		* @param  pEvent  The event dispatched by the TweenRunner.
		*/
		private function onEndTween(pEvent:TweenEvent):void {
			if (++_transitionIndex >= _transitions.length) {
				dispatchEvent(new TweenEvent(TweenEvent.END, this));
			}
		}
	
		/**
		* @description  Stops the transition.
		*/
		override public function halt():void {
			for each (var i:Transition in _transitions) {
				i.removeEventListener(TweenEvent.END, onEndTween);
				i.halt();
			}
		}
	
		/**
		* @description  Begins the transition.
		*/
		override public function start():void {
			_transitionIndex = 0;
			for each (var i:Transition in _transitions) {
				i.addEventListener(TweenEvent.END, onEndTween);
				i.clip = clip;
				i.start();
			}
		}
			
		/**
		* @description  Cleans up any timers that might be running.
		*/
		override public function die():void {
			super.die();
			_transitions = null;
		}
		
	}
	
}